package com.web.cms.doc.util;

import com.blue.core.util.NumberUtil;
import com.blue.core.util.UrlUtil;
import com.blue.integration.serve.Bucket;
import com.blue.integration.serve.StorageClient;
import com.blue.jdbc.Page;
import com.web.cms.doc.message.DocRequest;
import com.web.cms.doc.message.DocResponse;
import com.web.cms.doc.model.Doc;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DocConvertor
{
	private static Pattern[] fileKeyPatterns = new Pattern[]
			{ Pattern.compile("\\[!\\[.*\\]\\((.+)\\)\\]"), Pattern.compile("[^\\[]!\\[.*\\]\\((.+)\\)")};
	
	private DocConvertor()
	{
	}
	
	public static Doc toDoc(DocRequest request)
	{
		if (request == null)
			return null;
		
		Doc doc = new Doc();
		doc.setId(request.getId());
		doc.setChannelId(request.getChannelId());
		doc.setWebId(request.getWebId());
		doc.setUserId(request.getUserId());
		doc.setAuthor(request.getAuthor());
		doc.setTitle(request.getTitle());
		doc.setColor(request.getColor());
		doc.setPic(request.getPic());
		doc.setSummary(request.getSummary());
		doc.setContentUrl(request.getContentUrl());
		doc.setPublishTime(request.getPublishTime());
		doc.setCreateTime(request.getCreateTime());
		doc.setUpdateTime(request.getUpdateTime());
		doc.setSort(request.getSort());
		doc.setState(request.getState());
		doc.setViews(request.getViews());
		doc.setComments(request.getComments());
		doc.setFiles(request.getFiles());
		doc.setFileSize(request.getFileSize());
		
		doc.setContent(request.getContent());
		return doc;
	}
	
	public static DocResponse toResponse(Doc doc, StorageClient storageClient)
	{
		if (doc == null)
			return null;
		
		DocResponse response = new DocResponse();
		response.setId(doc.getId());
		response.setChannelId(doc.getChannelId());
		response.setWebId(doc.getWebId());
		response.setUserId(doc.getUserId());
		response.setAuthor(doc.getAuthor());
		response.setTitle(doc.getTitle());
		response.setColor(doc.getColor());
		response.setPic(doc.getPic());
		response.setSummary(doc.getSummary());
		response.setContentUrl(doc.getContentUrl());
		response.setPublishTime(doc.getPublishTime());
		response.setCreateTime(doc.getCreateTime());
		response.setUpdateTime(doc.getUpdateTime());
		response.setSort(doc.getSort());
		response.setState(doc.getState());
		response.setViews(doc.getViews());
		response.setComments(doc.getComments());
		response.setFiles(doc.getFiles());
		response.setFileSize(doc.getFileSize());
		
		response.setContent(doc.getContent());
		response.setFileSizeName(NumberUtil.byteFormat(doc.getFileSize().longValue()));
		response.setPicUrl(storageClient.getUrl(Bucket.CMS, doc.getPic()));
		return response;
	}
	
	public static List<DocResponse> toResponse(List<Doc> list, StorageClient storageClient)
	{
		List<DocResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;
		
		for (Doc doc : list)
		{
			responseList.add(toResponse(doc, storageClient));
		}
		return responseList;
	}
	
	public static Page<DocResponse> toResponse(Page<Doc> page, StorageClient storageClient)
	{
		Page<DocResponse> pageResponse = new Page<>();
		pageResponse.setCurPage(page.getCurPage());
		pageResponse.setRows(page.getRows());
		pageResponse.setSize(page.getSize());
		pageResponse.setStartRowNo(page.getStartRowNo());
		pageResponse.setTotalPage(page.getTotalPage());
		pageResponse.setObjectList(toResponse(page.getObjectList(), storageClient));
		return pageResponse;
	}
	
	public static List<String> parseFileKey(String content)
	{
		List<String> keyList = new ArrayList<>();
		
		for (Pattern pattern : fileKeyPatterns)
		{
			Matcher imgLinkMatcher = pattern.matcher(content);
			while (imgLinkMatcher.find())
			{
				int group = imgLinkMatcher.groupCount();
				for (int i = 1; i <= group; i++)
				{
					String m = imgLinkMatcher.group(i);
					if (m.indexOf(" ") != -1)
					{
						m = m.split(" ")[0];
					}
					m = UrlUtil.getFileName(m);
					keyList.add(m);
				}
			}
		}
		
		
		return keyList;
	}
	
}
